<?php

namespace app\index\controller;

use think\Controller;
use think\Request;

ini_set("display_errors", "on");

require_once dirname(__DIR__) . '/api_sdk/vendor/autoload.php';

use Aliyun\Core\Config;
use Aliyun\Core\Profile\DefaultProfile;
use Aliyun\Core\DefaultAcsClient;
use Aliyun\Api\Sms\Request\V20170525\SendSmsRequest;
use Aliyun\Api\Sms\Request\V20170525\SendBatchSmsRequest;
use Aliyun\Api\Sms\Request\V20170525\QuerySendDetailsRequest;
use app\common\model\Users;
use think\Session;

// 加载区域结点配置
Config::load();


class Entry extends Controller
{
    static $acsClient = null;

    /**
     * 取得AcsClient
     *
     * @return DefaultAcsClient
     */
    public static function getAcsClient()
    {
        //产品名称:云通信流量服务API产品,开发者无需替换
        $product = "Dysmsapi";

        //产品域名,开发者无需替换
        $domain = "dysmsapi.aliyuncs.com";

        // TODO 此处需要替换成开发者自己的AK (https://ak-console.aliyun.com/)
        $accessKeyId = "LTAImDu1jqxeglez"; // AccessKeyId

        $accessKeySecret = "EYZ4OvgFypFXPd0dSRRjiUIBRcQ8ey"; // AccessKeySecret

        // 暂时不支持多Region
        $region = "cn-hangzhou";

        // 服务结点
        $endPointName = "cn-hangzhou";


        if (static::$acsClient == null) {

            //初始化acsClient,暂不支持region化
            $profile = DefaultProfile::getProfile($region, $accessKeyId, $accessKeySecret);

            // 增加服务结点
            DefaultProfile::addEndpoint($endPointName, $region, $product, $domain);

            // 初始化AcsClient用于发起请求
            static::$acsClient = new DefaultAcsClient($profile);
        }
        return static::$acsClient;
    }


    public function phone()
    {
        if (IS_POST) {

            $post = input('post.');
            $mobile = $post['username'];
//        p($mobile);
            //获取手机号码
//        print_r($mobile);

//        $user = M('db_admin')->where(array('admin'=>$admin))->find();
//

            $request = new SendSmsRequest();

//接受号码
            $request->setPhoneNumbers($mobile);


            // 必填，设置签名名称，应严格按"签名名称"填写，请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/sign
            $request->setSignName("个人展示");


            // 必填，设置模板CODE，应严格按"模板CODE"填写, 请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/template
            $request->setTemplateCode("SMS_123668515");

//        /*************配置***************/
            $code = substr(md5(mt_rand()), 0, 4);

            $request->setTemplateParam(json_encode(array(  // 短信模板中字段的值
                "code" => $code,
                "product" => "dsd"
            ), JSON_UNESCAPED_UNICODE));

            // 可选，设置流水号
            $request->setOutId("yourOutId");

//        p($code);

            // 选填，上行短信扩展码（扩展码字段控制在7位或以下，无特殊需求用户请忽略此字段）
            $request->setSmsUpExtendCode("1234567");

            // 发起访问请求
            $acsResponse = static::getAcsClient()->getAcsResponse($request);


            //短信发送成功返回True，失败返回false
            Session::set('code', $code);

            if ($acsResponse && $acsResponse->Code == 'OK') {

                return array('status' => 2, 'msg' => '验证码发送成功', 'code' => $code);

            } else {
                return array('status' => 0, 'msg' => '验证码发送失败', 'code' => $code);
            }

        }
//        return $acsResponse;
    }


    public function register()
    {

        if (IS_POST) {
            $post = input('post.');
//判断用户名
            $userwhere = Users::where('username', '=', $post['username'])->find();

            if ($userwhere) {

                return array('status' => 0, 'msg' => '用户名已经存在');
            }
//判断验证码
            if (Session::get('code') != strtolower($post['code'])) {

                return array('status' => 0, 'msg' => '验证码错误');
            }

            $data = ['username' => $post['username'], 'password' => md5($post['password'])];

            $user = new Users();

            $user->save($data);

            Session::delete('code');

            return array('status' => 1, 'msg' => '注册成功，准备跳转');
        }


//        p($_POST);

//        p(Session::get());

        return view();

    }


}
